package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "套餐管理", description = "套餐相关操作")
@Slf4j
@RequestMapping("/admin/setmeal")
@EnableCaching
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Operation(description = "新增套餐",summary = "新增套餐")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功新增套餐")
    )
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result saveWithDish(@RequestBody SetmealDTO setmealDTO){
        log.info("保存菜品信息：{}", setmealDTO);
        setmealService.saveWithDish(setmealDTO);
        return Result.success();
    }

    @Operation(summary = "套餐分页查询",description = "返回套餐分页查询信息")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功获取套餐分页信息")
    )
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页查询信息：{}",setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @Operation(summary = "批量删除套餐",description = "批量删除套餐")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功删除套餐")
    )
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除套餐：{}",ids);
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    @Operation(summary = "根据id查询套餐",description = "根据id查询套餐")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功查询套餐信息")
    )
    @GetMapping("/{id}")
    @Cacheable(cacheNames = "setmealCache", key = "#id")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("根据id查询套餐：{}",id);
        SetmealVO setmealVO = setmealService.getByIdWithDish(id);
        return Result.success(setmealVO);
    }

    @Operation(summary = "修改套餐",description = "修改套餐")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功修改套餐")
    )
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐：{}",setmealDTO);
        setmealService.updateWithDish(setmealDTO);
        return Result.success();
    }

    @Operation(summary = "套餐起售停售",description = "套餐起售停售")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功修改套餐状态")
    )
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("套餐起售停售：{}",id);
        setmealService.startOrStop(status,id);
        return Result.success();
    }
}
